package com.ylsk.common.utils;


import com.ylsk.common.core.domain.AjaxResult;
import org.apache.commons.collections4.MapUtils;

import java.util.Date;
import java.util.Map;
import java.util.Set;

public class ActivitiUtils {

    private static  ActivitiUtils activitiUtils=new ActivitiUtils();

    private ActivitiUtils(){

    }

    public  static ActivitiUtils getInstance(){
        if(activitiUtils==null){
            activitiUtils=new ActivitiUtils();
        }
        return  activitiUtils;
    }

    public  static AjaxResult business2Variable(Map<String, Object> params, Object businessObj){
        if(businessObj !=null){
            try {
                if(businessObj instanceof Map){
                    Map beanMap = (Map) businessObj;
                    Set<Map.Entry<String,Object>> beanEntrySet = beanMap.entrySet();
                    beanEntrySet.forEach(entry->{
                        if(entry.getValue() != null &&(Long.class.isAssignableFrom(entry.getValue().getClass()) ||
                                Long.TYPE.isAssignableFrom(entry.getValue().getClass()))){
                            params.put(entry.getKey(),new Date(Long.valueOf(entry.getValue().toString())));
                        }else{
                            params.put(entry.getKey(),entry.getValue());
                        }

                    });
                }
                Map<String, Object> stringObjectMap = BeanUtils.bean2Map(businessObj);
                if(MapUtils.isNotEmpty(stringObjectMap)){
                    for(Map.Entry var:stringObjectMap.entrySet()){
                        if(var.getValue() != null &&(Long.class.isAssignableFrom(var.getValue().getClass()) ||
                                Long.TYPE.isAssignableFrom(var.getValue().getClass()))){
                            params.put(var.getKey().toString(),new Date(Long.valueOf(var.getValue().toString())));
                        }else{
                            params.put(var.getKey().toString(),var.getValue());
                        }

                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.error("业务对象有误,错误信息%s",e.getMessage());
            }
        }
        return  null;
    }


}
